<?php

require_once BaseUtil::getModulePath("base") . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("config") . '/services/MarketingMappingService.php';
require_once BaseUtil::getModulePath("config") . '/forms/MarketingMappingForm.php';

class Config_MarketingmappingController extends BaseController {
	
	public function init() {
		parent::init();
	}
	
	public function indexAction() {
		$marketingmappingService=new MarketingMappingService();
		$mappings=$marketingmappingService->getMappings($this->view,$_POST);
		$this->view->mappings=$mappings;
	}
	
	public function addAction() {
		$marketingmappingForm=new MarketingMappingForm();
		$this->view->form=$marketingmappingForm;
	}
	
	public function subaddAction() {
		$marketingmappingForm = new MarketingMappingForm();
        $marketingmappingService = new MarketingMappingService();
        if (!$marketingmappingForm->isValid($_POST)){
            $this->view->form = $marketingmappingForm;
            return $this->render('add');
        }
        $marketingmappingService->addMapping($marketingmappingForm);
        $this->_redirect ('/config/marketingmapping');
	}
	
	public function editAction() {
		$mappingId = $this->_getParam('id');
        $marketingmappingService = new MarketingMappingService();
        $mapping = $marketingmappingService->getMappingById($mappingId);
        $marketingmappingForm = new MarketingMappingForm(true);
        $marketingmappingForm->getElement("id")->setValue($mapping['id']);
        $marketingmappingForm->getElement("mapping_key")->setValue($mapping['mapping_key']);
        $marketingmappingForm->getElement("mapping_value")->setValue($mapping['mapping_value']);
        $this->view->form = $marketingmappingForm;
        $this->view->id = $mappingId;
	}
	
	
	public function subeditAction() {
		$id=HttpUtil::getPostString("id");
		$marketingmappingForm=new MarketingMappingForm(true,$id);
		$marketingmappingService=new MarketingMappingService();
		if(! $marketingmappingForm->isValid($_POST)){
			$this->view->form=$marketingmappingForm;
			return $this->render('edit');
		}
		$marketingmappingService->editMapping($marketingmappingForm);
		$this->_redirect('/config/marketingmapping');
	}
	
	
	public function deletesomeAction() {
		$idsStr=HttpUtil::getPostValue("selectIds");      
        $ids=explode(",",$idsStr);
        $marketingmappingService=new MarketingMappingService();
        $marketingmappingService->deleteMappings($ids);
        $this->_redirector->gotoUrl('/config/marketingmapping');
	}
	
	
	public function deleteAction() {
		$id=$this->_getParam('id');		
		$marketingmappingService=new MarketingMappingService();
		$marketingmappingService->delete($id);
		$this->_redirector->gotoUrl('/config/marketingmapping');
	}	
}